<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>视窗推荐管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <!--列表-->
    <el-button type="warning" @click="addShop" style="margin-bottom: 20px">添加推荐商品</el-button>
    <el-table size="small" :data="shopList" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;" max-height="600">
      <el-table-column align="center" prop="goodsName" label="商品昵称" width="140">
      </el-table-column>
      <el-table-column align="center" prop="coverImg" label="商品封面图片" width="150">
        <template slot-scope="scope" v-if="scope.row.coverImg">
          <el-image
            style="width: 100px"
            :src="scope.row.coverImg"
            fit="contain"></el-image>
        </template>
      </el-table-column>
      <!-- <el-table-column sortable align="center" prop="originalPrice" label="原价(进货价)" width="140">
      </el-table-column> -->
      <el-table-column sortable align="center" prop="sellPrice" label="售卖价（元）" width="150">
      </el-table-column>
      <!-- <el-table-column align="center" prop="countryOfOrigin" label="原产地" width="150"> -->
      </el-table-column>
      <el-table-column align="center" prop="shippingAddress" label="发货地" width="150">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="deleteShop(scope.$index, scope.row)">取消推荐</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加推荐商品" :visible.sync="editFormVisible" width="60%">
      <el-table size="small" :data="waitShopList" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;" max-height="400">
      <el-table-column sortable align="center" prop="goodsName" label="商品昵称" width="150">
      </el-table-column>
      <el-table-column align="center" prop="coverImg" label="商品封面图片" width="150">
        <template slot-scope="scope" v-if="scope.row.coverImg">
          <el-image
            style="width: 100px"
            :src="scope.row.coverImg"
            fit="contain"></el-image>
        </template>
      </el-table-column>
      <!-- <el-table-column sortable align="center" prop="originalPrice" label="原价(进货价)" width="150">
      </el-table-column> -->
      <el-table-column sortable align="center" prop="sellPrice" label="售卖价（元）" width="150">
      </el-table-column>
      <!-- <el-table-column sortable align="center" prop="countryOfOrigin" label="原产地" width="150"> -->
      </el-table-column>
      <el-table-column sortable align="center" prop="shippingAddress" label="发货地" width="150">
      </el-table-column>
      <el-table-column sortable align="center" prop="isRecommend" label="商品推荐状态" width="150">
        <template slot-scope="scope">
          {{scope.row.isRecommend ? '已推荐' : '未推荐'}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <span v-if="scope.row.isRecommend">已推荐</span>
          <el-button v-else size="mini" type="warning" @click="changeGood(scope.row)">加入推荐</el-button>
        </template>
      </el-table-column>
    </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="editFormVisible=false">取消</el-button>
        <el-button size="small" type="primary" @click="editFormVisible=false">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name:'Recommend',
  data() {
    return {
      editFormVisible: false, // 弹出框
      shopList: [], // 已推荐列表
      waitShopList: [], // 待推荐列表
      loading: false, //是显示加载
      pageQuery: {
        pageNum: 1,
        pageSize: -1
      }
    }
  },
  created() {
    this.shopRecommend()
  },
  methods: {
    /**
     * 已推荐列表
     */
    shopRecommend () {
      this.$api.shop.shopRecommend(this.pageQuery).then(res => {
        if (res.code === 200) {
          this.shopList = res.data.records
        }
      })
    },
    /**
     * 待推荐列表
     */
    shopCanRecommend () {
      this.$api.shop.shopCanRecommend(this.pageQuery).then(res => {
        if (res.code === 200) {
          this.waitShopList = res.data.records
        }
      })
    },
    /**
     * 添加推荐商品
     */
    addShop () {
      this.editFormVisible = true
      this.shopCanRecommend()
      this.shopRecommend()
    },
    /**
     * 取消推荐商品
     */
    deleteShop (index, shop) {
      let params = {
        id: shop.id
      }
      this.$api.shop.delRecommend(params).then(res => {
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '取消推荐成功!'
          })
          this.shopRecommend()
        }
      })
    },
    /**
     * 加入推荐
     */
    changeGood (shop) {
      let params = {
        id: shop.id
      }
      this.$api.shop.addRecommend(params).then(res => {
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '加入成功！'
          })
          this.shopCanRecommend()
        }
      })
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 